//
//  MPBNetWorking.h
//  MiaoPai
//
//  Created by jiaxuzhou on 2017/5/24.
//  Copyright © 2017年 jiaxuzhou. All rights reserved.
//

#import "MPBNetWorking.h"
#import "MPCURLSessionTask+MPCNetWork.h"
#import <objc/runtime.h>

const NSString *kMPBNetWorkingBindingWithOperation = @"kMPBNetWorkingBindingWithOperation";

static dispatch_queue_t mp_operate_queue;
void *operate_queue_key = &operate_queue_key;

void _mpqueue_block_async_on_networing_operate_queue(dispatch_block_t block) {
    
    if (dispatch_get_specific(operate_queue_key))
    {
        block();
    }
    else
    {
        dispatch_async(mp_operate_queue, block);
    }
//    dispatch_get_specific(operate_queue_key) ? block() : dispatch_async(mp_operate_queue, block);
}

@interface MPBNetWorking ()

/// 网络请求基类，提供请求对象
@property (nonatomic, strong) MPBNetWorkBase *netWorkBase;


@end


@implementation MPBNetWorking

+(void)load
{
    mp_operate_queue = dispatch_queue_create("com.miaopai.networking.operate", NULL);
    dispatch_queue_set_specific(mp_operate_queue, operate_queue_key, operate_queue_key, NULL);
}

#pragma mark - ******************************************* 操作方法 **********************

- (void)cancelSelf
{
    MPURLSessionTask *taskCurrentt = [self getSelfTask];
    @synchronized(self)
    {
        [[[self class] allTasks] enumerateObjectsWithOptions:NSEnumerationReverse
                                                  usingBlock:^(MPURLSessionTask * _Nonnull task,
                                                               NSUInteger idx,
                                                               BOOL * _Nonnull stop)
        {
            if ([task isKindOfClass:[MPURLSessionTask class]]
                && task == taskCurrentt)
            {
                [(MPURLSessionTask *)task setIsCancel:YES];
                [task cancel];
                [[[self class] allTasks] removeObject:task];
                
                return;
            }
        }];
    };
}

+ (void)cancelAllRequest
{
    [MPBNetWorkBase cancelAllRequest];
}

+ (void)cancelRequestWithURL:(NSString *)url
{
    [MPBNetWorkBase cancelRequestWithURL:url];
}

+ (NSUInteger)tasksCount
{
    return [MPBNetWorkBase tasksCount];
}

+ (NSMutableArray *)allTasks
{
    return [MPBNetWorkBase allTasks];
}


- (MPURLSessionTask *)getSelfTask
{
    MPURLSessionTask *task = objc_getAssociatedObject(self, (__bridge const void *)(kMPBNetWorkingBindingWithOperation));
    
    if (task && [task isKindOfClass:[MPURLSessionTask class]])
    {
        return task;
    }
    
    return nil;
}

- (void)setResponseType:(MPE_NetResponseType)type
{
    [self.netWorkBase setResponseType:type];
}

- (void)setNoNetWorkBlock:(BOOL (^)(void))noNetWorkBlock
{
    if (_noNetWorkBlock != noNetWorkBlock)
    {
        _noNetWorkBlock = [noNetWorkBlock copy];
        
        self.netWorkBase.noNetWorkInBaseBlock = _noNetWorkBlock;
    }
}

- (void)setOldApi:(BOOL)oldApi
{
    self.netWorkBase.oldApi = oldApi;
}

-(void)setResponseSecType:(MPE_NetRespSecType)responseSecType
{
    self.netWorkBase.responseSecType = responseSecType;
}

- (void)setReqByTotalUrl:(BOOL)reqByTotalUrl
{
    _reqByTotalUrl = reqByTotalUrl;
    self.netWorkBase.reqByTotalUrl = reqByTotalUrl;
}

#pragma mark - ******************************************* 普通网络请求方法 **********************

- (void)netWorkWithsuccess:(MPHNetFinishedBlock)success
                            failed:(MPHNetFinishedBlock)failed
                            cached:(void (^)(NSDictionary *cachedObject))cached
{
    MPE_NetMethod method = MPE_POST;
    if ([self respondsToSelector:@selector(requestMethod)])
    {
        method = [self requestMethod];
    }
    
    [self netWorkWithMethod:method
                        URL:nil
                     params:nil
           isShowNoNetAlert:MPDefautShowNoNetViewState
                  isEncrypt:MPDefautIsEncrypt
                    success:success
                     failed:failed
                     cached:cached];
}

- (void)netWorkWithsuccess:(MPHNetFinishedBlock)success
                    failed:(MPHNetFinishedBlock)failed
                 noNetWork:(MPHNoNetWorkNewBlock)noNetWork
                    cached:(void (^)(NSDictionary *cachedObject))cached
{
    MPE_NetMethod method = MPE_POST;
    if ([self respondsToSelector:@selector(requestMethod)])
    {
        method = [self requestMethod];
    }
    
    [self netWorkWithMethod:method
                        URL:nil
                     params:nil
           isShowNoNetAlert:MPDefautShowNoNetViewState
                  isEncrypt:MPDefautIsEncrypt
                    success:success
                     failed:failed
                  noNetWork:noNetWork
                     cached:cached];
}

/// 最全的
- (void)netWorkWithMethod:(MPE_NetMethod)method
                              URL:(NSString *)url
                           params:(NSDictionary *)dictParams
                 isShowNoNetAlert:(BOOL)isShowNoNetAlert
                        isEncrypt:(BOOL)isEncrypt
                          success:(MPHNetFinishedBlock)success
                           failed:(MPHNetFinishedBlock)failed
                           cached:(void (^)(NSDictionary *cachedObject))cached
{
    [self netWorkWithMethod:method
                        URL:url
                     params:dictParams
           isShowNoNetAlert:isShowNoNetAlert
                  isEncrypt:isEncrypt
                    success:success
                     failed:failed
                  noNetWork:nil
                     cached:cached];
}

/// 最全的New
- (void)netWorkWithMethod:(MPE_NetMethod)method
                      URL:(NSString *)url
                   params:(NSDictionary *)dictParams
         isShowNoNetAlert:(BOOL)isShowNoNetAlert
                isEncrypt:(BOOL)isEncrypt
                  success:(MPHNetFinishedBlock)success
                   failed:(MPHNetFinishedBlock)failed
                noNetWork:(MPHNoNetWorkNewBlock)noNetWork
                   cached:(void (^)(NSDictionary *cachedObject))cached
{
    _mpqueue_block_async_on_networing_operate_queue(^
                                                    
    {
        @autoreleasepool
        {
            [self configeNetWorkFromOverload];
            MPURLSessionTask *operation = nil;
            switch (method)
            {
                case MPE_POST:
                {
                    if (noNetWork)
                    {
                        operation = [self __post:isShowNoNetAlert
                                       isEncrypt:isEncrypt
                                        priority:_priority
                                             URL:url
                                          params:dictParams
                                         success:success
                                          failed:failed
                                       noNetWork:noNetWork
                                          cached:cached];
                    }
                    else
                    {
                        operation = [self __post:isShowNoNetAlert
                                       isEncrypt:isEncrypt
                                        priority:_priority
                                             URL:url
                                          params:dictParams
                                         success:success
                                          failed:failed
                                          cached:cached];
                    }
                }
                    break;
                case MPE_GET:
                {
                    if (noNetWork)
                    {
                        operation = [self __get:isShowNoNetAlert
                                      isEncrypt:isEncrypt
                                       priority:_priority
                                            URL:url
                                         params:dictParams
                                        success:success
                                         failed:failed
                                      noNetWork:noNetWork
                                         cached:cached];
                    }
                    else
                    {
                        operation = [self __get:isShowNoNetAlert
                                      isEncrypt:isEncrypt
                                       priority:_priority
                                            URL:url
                                         params:dictParams
                                        success:success
                                         failed:failed
                                         cached:cached];
                    }
                }
                    break;
            }
            
            if (operation)
            {
                objc_setAssociatedObject(self,
                                         (__bridge const void *)(kMPBNetWorkingBindingWithOperation),
                                         operation,
                                         OBJC_ASSOCIATION_RETAIN_NONATOMIC);
            }
            
            
        }
        
    });
}

- (MPURLSessionTask *)__post:(BOOL)isShowNoNetAlert
                   isEncrypt:(BOOL)isEncrypt
                    priority:(MPE_NetWorkPriority)priority
                         URL:(NSString *)url
                      params:(NSDictionary *)dictParams
                     success:(MPHNetFinishedBlock)success
                      failed:(MPHNetFinishedBlock)failed
                      cached:(void (^)(NSDictionary *cachedObject))cached
{
    __weak typeof(self) weakSelf = self;
    MPURLSessionTask *operation = [self.netWorkBase POST:url
                                              parameters:dictParams
                                        isShowNoNetAlert:isShowNoNetAlert
                                               isEncrypt:isEncrypt
                                                priority:priority
                                                 success:^(MPURLSessionTask *operation, id responseObject)
                                   {
                                       
                                       switch ([[weakSelf.netWorkBase class] checkState4Result:responseObject
                                                                                          task:operation
                                                                  isShowToastView4NetWorkState:[operation isShowToastView4NetWorkState]
                                                                                        cached:cached
                                                                           sessionTimeoutBlock:[operation sessionTimeoutBlock]])
                                       {
                                           case MPE_NetWorkState_Successs:
                                           {
                                               weakSelf.originalData = responseObject;
                                               weakSelf.responseObject = [[weakSelf.netWorkBase class] resetResponse:responseObject];
                                               [weakSelf netWorkSuccessPreHandleIfExist];
                                               if (success)
                                               {
                                                   success(weakSelf, operation);
                                               }
                                           }
                                               break;
                                           case MPE_NetWorkState_Fail:
                                           {
                                               weakSelf.responseObject = responseObject;
                                               weakSelf.originalData = responseObject;
                                               [weakSelf netWorkFailedPreHandleIfExist];
                                               if (failed) failed(weakSelf, operation);
                                           }
                                               break;
                                           case MPE_NetWorkState_NoBack: break;
                                       };
                                   }
                                                 failure:^(MPURLSessionTask *operation, NSError *error)
                                   {
                                       MPE_NetWorkState state = [[weakSelf.netWorkBase class] checkState4Result:nil
                                                                                                           task:operation
                                                                                   isShowToastView4NetWorkState:[operation isShowToastView4NetWorkState]
                                                                                                         cached:cached
                                                                                            sessionTimeoutBlock:[operation sessionTimeoutBlock]];
                                       if (state != MPE_NetWorkState_NoBack)
                                       {
                                           [weakSelf netWorkFailedPreHandleIfExist];
                                           weakSelf.responseObject = nil;
                                           weakSelf.originalData = nil;
                                           if (failed) failed(weakSelf, operation);
                                       }
                                   }
                                                  cached:^(NSDictionary *cachedObject)
                                   {
                                       weakSelf.cacheData = cachedObject;
                                       [weakSelf netWorkCachePreHandleIfExist];
                                       
                                       if (cached)
                                       {
                                           cached(cachedObject);
                                       }
                                   }];
    
    [self configOperaion:operation];
    
    return operation;
}

- (MPURLSessionTask *)__post:(BOOL)isShowNoNetAlert
                   isEncrypt:(BOOL)isEncrypt
                    priority:(MPE_NetWorkPriority)priority
                         URL:(NSString *)url
                      params:(NSDictionary *)dictParams
                     success:(MPHNetFinishedBlock)success
                      failed:(MPHNetFinishedBlock)failed
                   noNetWork:(MPHNoNetWorkNewBlock)noNetWork
                      cached:(void (^)(NSDictionary *cachedObject))cached
{
    __weak typeof(self) weakSelf = self;
    MPURLSessionTask *operation = [self.netWorkBase POST:url
                                              parameters:dictParams
                                        isShowNoNetAlert:isShowNoNetAlert
                                               isEncrypt:isEncrypt
                                                priority:priority
                                                 success:^(MPURLSessionTask *operation, id responseObject)
    {
        
        switch ([[weakSelf.netWorkBase class] checkState4Result:responseObject
                                            task:operation
                    isShowToastView4NetWorkState:[operation isShowToastView4NetWorkState]
                                           cached:cached
                             sessionTimeoutBlock:[operation sessionTimeoutBlock]])
        {
            case MPE_NetWorkState_Successs:
            {
                weakSelf.originalData = responseObject;
                weakSelf.responseObject = [[weakSelf.netWorkBase class] resetResponse:responseObject];
                [weakSelf netWorkSuccessPreHandleIfExist];
                if (success)
                {
                    success(weakSelf, operation);
                }
            }
                break;
            case MPE_NetWorkState_Fail:
            {
                weakSelf.responseObject = responseObject;
                weakSelf.originalData = responseObject;
                [weakSelf netWorkFailedPreHandleIfExist];
                if (failed) failed(weakSelf, operation);
            }
                break;
            case MPE_NetWorkState_NoBack: break;
        };
    }
                                                 failure:^(MPURLSessionTask *operation, NSError *error)
    {
        MPE_NetWorkState state = [[weakSelf.netWorkBase class] checkState4Result:nil
                                                           task:operation
                                   isShowToastView4NetWorkState:[operation isShowToastView4NetWorkState]
                                                         cached:cached
                                            sessionTimeoutBlock:[operation sessionTimeoutBlock]];
        if (state != MPE_NetWorkState_NoBack)
        {
            weakSelf.responseObject = nil;
            weakSelf.originalData = nil;
            
            [weakSelf netWorkFailedPreHandleIfExist];
            if (failed) failed(weakSelf, operation);
        }
    } noNetWork:^BOOL
    {
        return noNetWork(weakSelf);
    }
                                                  cached:^(NSDictionary *cachedObject) {
        weakSelf.cacheData = cachedObject;
        [weakSelf netWorkCachePreHandleIfExist];
        
        if (cached)
        {
            cached(cachedObject);
        }
                                                  }];
    
    [self configOperaion:operation];
    
    return operation;
}

- (MPURLSessionTask *)__get:(BOOL)isShowNoNetAlert
                  isEncrypt:(BOOL)isEncrypt
                   priority:(MPE_NetWorkPriority)priority
                        URL:(NSString *)url
                     params:(NSDictionary *)dictParams
                    success:(MPHNetFinishedBlock)success
                     failed:(MPHNetFinishedBlock)failed
                     cached:(void (^)(NSDictionary *cachedObject))cached
{
    __weak typeof(self) weakSelf = self;
    
    
    MPURLSessionTask *operation = [self.netWorkBase GET:url
                                             parameters:dictParams
                                       isShowNoNetAlert:isShowNoNetAlert
                                              isEncrypt:isEncrypt
                                               priority:priority
                                                success:^(MPURLSessionTask *operation,
                                                          id responseObject)
                                   {
                                       __strong typeof(weakSelf) strongSelf = weakSelf;
                                       switch ([[strongSelf.netWorkBase class] checkState4Result:responseObject
                                                                                            task:operation
                                                                    isShowToastView4NetWorkState:[operation isShowToastView4NetWorkState]
                                                                                          cached:cached
                                                                             sessionTimeoutBlock:[operation sessionTimeoutBlock]])
                                       {
                                           case MPE_NetWorkState_Successs:
                                           {
                                               strongSelf.originalData = responseObject;
                                               strongSelf.responseObject = [[strongSelf.netWorkBase class] resetResponse:responseObject];
                                               [strongSelf netWorkSuccessPreHandleIfExist];
                                               
                                               MPHNetFinishedBlock successTemp = success;
                                               if (successTemp)
                                               {
                                                   successTemp(strongSelf, operation);
                                               }
                                           }
                                               break;
                                           case MPE_NetWorkState_Fail:
                                           {
                                               strongSelf.responseObject = responseObject;
                                               strongSelf.originalData = responseObject;
                                               [strongSelf netWorkFailedPreHandleIfExist];
                                               
                                               MPHNetFinishedBlock failedTemp = failed;
                                               if (failedTemp)
                                               {
                                                   failedTemp(strongSelf, operation);
                                               }
                                           }
                                               break;
                                           case MPE_NetWorkState_NoBack: break;
                                       };
                                       
                                   }
                                                failure:^(MPURLSessionTask *operation, NSError *error)
                                   {
                                       __strong typeof(weakSelf) strongSelf = weakSelf;
                                       MPE_NetWorkState state = [[strongSelf.netWorkBase class] checkState4Result:nil
                                                                                                             task:operation
                                                                                     isShowToastView4NetWorkState:[operation isShowToastView4NetWorkState]
                                                                                                           cached:cached
                                                                                              sessionTimeoutBlock:[operation sessionTimeoutBlock]];
                                       if (state != MPE_NetWorkState_NoBack)
                                       {
                                           [strongSelf netWorkFailedPreHandleIfExist];
                                           
                                           MPHNetFinishedBlock failedTemp = failed;
                                           weakSelf.responseObject = nil;
                                           weakSelf.originalData = nil;
                                           
                                           if (failedTemp)
                                           {
                                               failedTemp(strongSelf, operation);
                                           }
                                       }
                                   }
                                                 cached:^(NSDictionary *cachedObject)
                                   {
                                       weakSelf.cacheData = cachedObject;
                                       
                                       [weakSelf netWorkCachePreHandleIfExist];
                                       
                                       void (^cachedTemp)(NSDictionary *cachedObject) = cached;
                                       if (cachedTemp)
                                       {
                                           cachedTemp(cachedObject);
                                       }
                                   }];
    
    [self configOperaion:operation];
    
    return operation;
}

- (MPURLSessionTask *)__get:(BOOL)isShowNoNetAlert
                  isEncrypt:(BOOL)isEncrypt
                   priority:(MPE_NetWorkPriority)priority
                        URL:(NSString *)url
                     params:(NSDictionary *)dictParams
                    success:(MPHNetFinishedBlock)success
                     failed:(MPHNetFinishedBlock)failed
                  noNetWork:(MPHNoNetWorkNewBlock)noNetWork
                     cached:(void (^)(NSDictionary *cachedObject))cached
{
    __weak typeof(self) weakSelf = self;
    
    
    MPURLSessionTask *operation = [self.netWorkBase GET:url
                                             parameters:dictParams
                                       isShowNoNetAlert:isShowNoNetAlert
                                              isEncrypt:isEncrypt
                                               priority:priority
                                                success:^(MPURLSessionTask *operation,
                                                                id responseObject) 
    {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        switch ([[strongSelf.netWorkBase class] checkState4Result:responseObject
                                             task:operation
                     isShowToastView4NetWorkState:[operation isShowToastView4NetWorkState]
                                           cached:cached
                              sessionTimeoutBlock:[operation sessionTimeoutBlock]])
        {
            case MPE_NetWorkState_Successs:
            {
                strongSelf.originalData = responseObject;
                strongSelf.responseObject = [[strongSelf.netWorkBase class] resetResponse:responseObject];
                [strongSelf netWorkSuccessPreHandleIfExist];
                
                MPHNetFinishedBlock successTemp = success;
                if (successTemp)
                {
                    successTemp(strongSelf, operation);
                }
            }
                break;
            case MPE_NetWorkState_Fail:
            {
                strongSelf.responseObject = responseObject;
                strongSelf.originalData = responseObject;
                [strongSelf netWorkFailedPreHandleIfExist];
                
                MPHNetFinishedBlock failedTemp = failed;
                if (failedTemp)
                {
                    failedTemp(strongSelf, operation);
                }
            }
                break;
            case MPE_NetWorkState_NoBack: break;
        };
         
        } 
                                                      failure:^(MPURLSessionTask *operation, NSError *error)
    {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        MPE_NetWorkState state = [[strongSelf.netWorkBase class] checkState4Result:nil
                                                           task:operation
                                   isShowToastView4NetWorkState:[operation isShowToastView4NetWorkState]
                                                         cached:cached
                                            sessionTimeoutBlock:[operation sessionTimeoutBlock]];
        if (state != MPE_NetWorkState_NoBack)
        {
            [strongSelf netWorkFailedPreHandleIfExist];
            
            MPHNetFinishedBlock failedTemp = failed;
            weakSelf.responseObject = nil;
            weakSelf.originalData = nil;
            
            if (failedTemp)
            {
                failedTemp(strongSelf, operation);
            }
        }
    }
                                              noNetWork:^BOOL
                                   {
                                       return noNetWork(weakSelf);
                                   }
                                                 cached:^(NSDictionary *cachedObject)
    {
        weakSelf.cacheData = cachedObject;
        
        [weakSelf netWorkCachePreHandleIfExist];

        void (^cachedTemp)(NSDictionary *cachedObject) = cached;
        if (cachedTemp)
        {
            cachedTemp(cachedObject);
        }
    }];
    
    [self configOperaion:operation];
    
    return operation;
}


#pragma mark - ******************************************* 上传文件网络请求方法 **********************

- (void)netWorkUploadFile:(id)uploadFile
                  success:(MPHNetFinishedBlock)success
                  failure:(MPHNetFinishedBlock)failed
{
    [self netWorkUploadFile:uploadFile
                     length:0
                    destUrl:nil
                   fileName:@"uploadFile"
                 parameters:nil
                    success:success
                    failure:failed];
}

- (void)netWorkUploadFile:(id)uploadFile
                  destUrl:(NSString *)destUrl
               parameters:(NSDictionary *)parameters
                  success:(MPHNetFinishedBlock)success
                  failure:(MPHNetFinishedBlock)failed
{
    [self netWorkUploadFile:uploadFile
                     length:0
                    destUrl:destUrl
                   fileName:@"uploadFile"
                 parameters:parameters
                    success:success
                    failure:failed];
}

- (void)netWorkUploadFile:(id)uploadFile
                 fileName:(NSString *)fileName
               parameters:(NSDictionary *)parameters
                  success:(MPHNetFinishedBlock)success
                  failure:(MPHNetFinishedBlock)failed
{
    [self netWorkUploadFile:uploadFile
                     length:0
                    destUrl:nil
                   fileName:fileName
                 parameters:parameters
                    success:success
                    failure:failed];
}

- (void)netWorkUploadFile:(id)uploadFile
                  destUrl:(NSString *)destUrl
                 fileName:(NSString *)fileName
               parameters:(NSDictionary *)parameters
                  success:(MPHNetFinishedBlock)success
                  failure:(MPHNetFinishedBlock)failed
{
    [self netWorkUploadFile:uploadFile
                     length:0
                    destUrl:destUrl
                   fileName:fileName
                 parameters:parameters
                    success:success
                    failure:failed];
}

- (void)netWorkUploadFile:(id)uploadFile
                   length:(int64_t)length
                  destUrl:(NSString *)destUrl
                 fileName:(NSString *)fileName
               parameters:(NSDictionary *)parameters
                  success:(MPHNetFinishedBlock)success
                  failure:(MPHNetFinishedBlock)failed
{
    [self netWorkUploadFile:uploadFile
                     length:length
                    destUrl:destUrl
              paramFileName:fileName
                   fileName:fileName
                 parameters:parameters
                  isEncrypt:MPDefautIsEncrypt
                    success:success
                    failure:failed];
}

- (void)netWorkUploadFile:(id)uploadFile
                   length:(int64_t)length
                  destUrl:(NSString *)destUrl
            paramFileName:(NSString *)paramFileName
                 fileName:(NSString *)fileName
               parameters:(NSDictionary *)parameters
                isEncrypt:(BOOL)isEncrypt
                  success:(MPHNetFinishedBlock)success
                  failure:(MPHNetFinishedBlock)failed
{
    NSParameterAssert([uploadFile isKindOfClass:[NSString class]]
                      || [uploadFile isKindOfClass:[NSURL class]]
                      || [uploadFile isKindOfClass:[NSData class]]
                      || [uploadFile isKindOfClass:[NSInputStream class]]);
    
    [self netWorkUploadFileTo:destUrl
                   parameters:parameters
                    isEncrypt:isEncrypt
    constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
     {
         if ([uploadFile isKindOfClass:[NSString class]])
         {
             NSError *error;
             NSData *fileData = [NSData dataWithContentsOfFile:uploadFile
                                                       options:NSDataReadingMappedIfSafe
                                                         error:&error];
             
             
             if (!error)
             {
                 [formData appendPartWithFileData:fileData
                                             name:fileName
                                         fileName:paramFileName
                                         mimeType:@"image/jpeg"];
             }
         }
         else if ([uploadFile isKindOfClass:[NSData class]])
         {
             [formData appendPartWithFileData:uploadFile
                                         name:paramFileName
                                     fileName:fileName
                                     mimeType:@"image/jpeg"];
         }
         else if ([uploadFile isKindOfClass:[NSURL class]])
         {
             [formData appendPartWithFileURL:uploadFile name:fileName fileName:fileName mimeType:@"image/jpeg" error:NULL];
         }
         else if ([uploadFile isKindOfClass:[NSInputStream class]])
         {
//             NSParameterAssert(length>0);
             [formData appendPartWithInputStream:uploadFile
                                            name:fileName
                                        fileName:fileName
                                          length:length
                                        mimeType:@"image/jpeg"];
         }
     }
                      success:success
                      failure:failed];
}

- (void)netWorkUploadFileTo:(NSString *)destUrl
                 parameters:(NSDictionary *)parameters
                  isEncrypt:(BOOL)isEncrypt
  constructingBodyWithBlock:(void (^)(id<AFMultipartFormData>))block
                    success:(MPHNetFinishedBlock)success
                    failure:(MPHNetFinishedBlock)failed
{
    _mpqueue_block_async_on_networing_operate_queue(^
    {
        [self configeNetWorkFromOverload];
        
        __weak typeof(self) weakSelf = self;
        MPURLSessionTask *operation = [self.netWorkBase POST:destUrl
                                                  parameters:parameters
                                            isShowNoNetAlert:NO
                                                   isEncrypt:isEncrypt
                                                    priority:_priority
                                   constructingBodyWithBlock:block
                                                     success:^(MPURLSessionTask *operation, id responseObject)
                                       {
                                           __strong typeof(weakSelf) strongSelf = weakSelf;
                                           switch ([[strongSelf.netWorkBase class] checkState4Result:responseObject
                                                                                                task:operation
                                                                        isShowToastView4NetWorkState:[operation isShowToastView4NetWorkState]
                                                                                              cached:nil
                                                                                 sessionTimeoutBlock:[operation sessionTimeoutBlock]])
                                           {
                                               case MPE_NetWorkState_Successs:
                                               {
                                                   strongSelf.originalData = responseObject;
                                                   strongSelf.responseObject = [[strongSelf.netWorkBase class] resetResponse:responseObject];
                                                   [strongSelf netWorkSuccessPreHandleIfExist];
                                                   
                                                   MPHNetFinishedBlock successTemp = success;
                                                   if (successTemp)
                                                   {
                                                       successTemp(strongSelf, operation);
                                                   }
                                               }
                                                   break;
                                               case MPE_NetWorkState_Fail:
                                               {
                                                   strongSelf.responseObject = responseObject;
                                                   strongSelf.originalData = responseObject;
                                                   [strongSelf netWorkFailedPreHandleIfExist];
                                                   
                                                   MPHNetFinishedBlock failedTemp = failed;
                                                   if (failedTemp)
                                                   {
                                                       failedTemp(strongSelf, operation);
                                                   }
                                               }
                                                   break;
                                               case MPE_NetWorkState_NoBack:
                                                   break;
                                           };
                                       }
                                                     failure:^(MPURLSessionTask *operation, NSError *error)
                                       {
                                           __strong typeof(weakSelf) strongSelf = weakSelf;
                                           MPE_NetWorkState state = [[strongSelf.netWorkBase class] checkState4Result:nil
                                                                                                                 task:operation
                                                                                         isShowToastView4NetWorkState:[operation isShowToastView4NetWorkState]
                                                                                                               cached:nil
                                                                                                  sessionTimeoutBlock:[operation sessionTimeoutBlock]];
                                           if (state != MPE_NetWorkState_NoBack)
                                           {
                                               [strongSelf netWorkFailedPreHandleIfExist];
                                               
                                               MPHNetFinishedBlock failedTemp = failed;
                                               if (failedTemp)
                                               {
                                                   failedTemp(strongSelf, operation);
                                               }
                                           }
                                       }];
        
        [self configOperaion:operation];
    });
}


#pragma mark - ******************************************* 同步网络请求方法 **********************

- (id)netWorkSyncWithMethod:(MPE_NetMethod)method
                        URL:(NSString *)url
                     params:(NSDictionary *)dictParams
           isShowNoNetAlert:(BOOL)isShowNoNetAlert
                  isEncrypt:(BOOL)isEncrypt
                    success:(MPHNetFinishedBlock4Sync)success
                     failed:(MPHNetFinishedBlock4Sync)failed
                     cached:(void (^)(NSDictionary *cachedObject))cached
{
    [self configeNetWorkFromOverload];
    
    __weak typeof(self) weakSelf = self;
    
    /// 同步请求的回调参数和普通异步的回调参数不一样，代码不好复用
    NSString *strMethod = @"POST";
    switch (method) {
        case MPE_POST:
            strMethod = @"POST";
            break;
        case MPE_GET:
            strMethod = @"GET";
            break;
    }
    
    return [self.netWorkBase netWorkSyncWithMethod:strMethod
                                               URL:url
                                            params:dictParams
                                  isShowNoNetAlert:isShowNoNetAlert
                                         isEncrypt:isEncrypt
                                          priority:_priority
                                           success:^(id responseObject)
    {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        switch ([[strongSelf.netWorkBase class] checkState4Result:responseObject
                                            task:nil
                    isShowToastView4NetWorkState:strongSelf.isShowToastView4NetWorkState
                                           cached:cached
                             sessionTimeoutBlock:strongSelf.sessionTimeoutBlock])
        {
            case MPE_NetWorkState_Successs:
            {
                strongSelf.originalData = responseObject;
                strongSelf.responseObject = [[strongSelf.netWorkBase class] resetResponse:responseObject];
                [strongSelf netWorkSuccessPreHandleIfExist];
                
                MPHNetFinishedBlock4Sync successTemp = success;
                if (successTemp)
                {
                    successTemp(strongSelf, strongSelf.responseObject);
                }
            }
                break;
            case MPE_NetWorkState_Fail:
            {
                strongSelf.responseObject = responseObject;
                strongSelf.originalData = responseObject;
                [strongSelf netWorkFailedPreHandleIfExist];
                
                MPHNetFinishedBlock4Sync failedTemp = failed;
                if(failedTemp)
                {
                    failedTemp(strongSelf, responseObject);
                }
            }
                break;
            case MPE_NetWorkState_NoBack: break;
        };
    }
                                           failure:^(id responseObject)
    {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        MPE_NetWorkState state = [[strongSelf.netWorkBase class] checkState4Result:nil
                                                           task:nil
                                   isShowToastView4NetWorkState:strongSelf.isShowToastView4NetWorkState
                                                         cached:cached
                                            sessionTimeoutBlock:strongSelf.sessionTimeoutBlock];
        strongSelf.responseObject = responseObject;
        strongSelf.originalData = responseObject;
        if (state != MPE_NetWorkState_NoBack)
        {
            [strongSelf netWorkFailedPreHandleIfExist];
            
            MPHNetFinishedBlock4Sync failedTemp = failed;
            if (failedTemp)
            {
                failedTemp(strongSelf, responseObject);
            }
        }
    } cached:^(NSDictionary *cachedObject)
    {
        weakSelf.cacheData = cachedObject;

        [weakSelf netWorkCachePreHandleIfExist];

        void (^cachedTemp)(NSDictionary *cachedObject) = cached;
        if (cachedTemp)
        {
            cachedTemp(cachedObject);
        }
    }];
}


#pragma mark - ******************************************* 类方法 - 普通网络请求方法 **********************

+ (instancetype)netWorkWithMethod:(MPE_NetMethod)method
                              URL:(NSString *)url
                           params:(NSDictionary *)dictParams
                          success:(MPHNetFinishedBlockStatic)success
                           failed:(MPHNetFinishedBlockStatic)failed
{
    return [self netWorkWithMethod:method
                               URL:url
                            params:dictParams
                  isShowNoNetAlert:MPDefautShowNoNetViewState
      isShowToastView4NetWorkState:MPDefautShowToastState
                   timeoutInterval:MPDefaultRequestTimeoutInterval
                         isEncrypt:MPDefautIsEncrypt
                          priority:MPDefaultRequestPriority
                           success:success
                            failed:failed
                            cached:nil
                    sessionTimeOut:nil];
}

+ (instancetype)netWorkWithMethod:(MPE_NetMethod)method
                              URL:(NSString *)url
                           params:(NSDictionary *)dictParams
                           oldApi:(BOOL)oldApi
                          success:(MPHNetFinishedBlockStatic)success
                           failed:(MPHNetFinishedBlockStatic)failed
{
    return [self netWorkWithMethod:method
                               URL:url
                            params:dictParams
                  isShowNoNetAlert:MPDefautShowNoNetViewState
      isShowToastView4NetWorkState:MPDefautShowToastState
                   timeoutInterval:MPDefaultRequestTimeoutInterval
                         isEncrypt:MPDefautIsEncrypt
                            oldApi:oldApi
                          priority:MPDefaultRequestPriority
                           success:success
                            failed:failed
                            cached:nil
                    sessionTimeOut:nil];
}

+ (instancetype)netWorkWithMethod:(MPE_NetMethod)method
                              URL:(NSString *)url
                           params:(NSDictionary *)dictParams
                 isShowNoNetAlert:(BOOL)isShowNoNetAlert
                          success:(MPHNetFinishedBlockStatic)success
                           failed:(MPHNetFinishedBlockStatic)failed
{
    return [self netWorkWithMethod:method
                               URL:url
                            params:dictParams
                  isShowNoNetAlert:isShowNoNetAlert
      isShowToastView4NetWorkState:MPDefautShowToastState
                   timeoutInterval:MPDefaultRequestTimeoutInterval
                         isEncrypt:MPDefautIsEncrypt
                          priority:MPDefaultRequestPriority
                           success:success
                            failed:failed
                            cached:nil
                    sessionTimeOut:nil];
}

+ (instancetype)netWorkWithMethod:(MPE_NetMethod)method
                              URL:(NSString *)url
                           params:(NSDictionary *)dictParams
     isShowToastView4NetWorkState:(BOOL)isShowToastView4NetWorkState
                          success:(MPHNetFinishedBlockStatic)success
                           failed:(MPHNetFinishedBlockStatic)failed
{
    return [self netWorkWithMethod:method
                               URL:url
                            params:dictParams
                  isShowNoNetAlert:MPDefautShowNoNetViewState
      isShowToastView4NetWorkState:isShowToastView4NetWorkState
                   timeoutInterval:MPDefaultRequestTimeoutInterval
                         isEncrypt:MPDefautIsEncrypt
                          priority:MPDefaultRequestPriority
                           success:success
                            failed:failed
                            cached:nil
                    sessionTimeOut:nil];
}

+ (instancetype)netWorkWithMethod:(MPE_NetMethod)method
                         priority:(MPE_NetWorkPriority)priority
                              URL:(NSString *)url
                           params:(NSDictionary *)dictParams
                          success:(MPHNetFinishedBlockStatic)success
                           failed:(MPHNetFinishedBlockStatic)failed
                           cached:(void (^)(NSDictionary *cachedObject))cached
{
    return [self netWorkWithMethod:method
                               URL:url
                            params:dictParams
                  isShowNoNetAlert:MPDefautShowNoNetViewState
      isShowToastView4NetWorkState:MPDefautShowToastState
                   timeoutInterval:MPDefaultRequestTimeoutInterval
                         isEncrypt:MPDefautIsEncrypt
                          priority:priority
                           success:success
                            failed:failed
                            cached:cached
                    sessionTimeOut:nil];

}

+ (instancetype)netWorkWithMethod:(MPE_NetMethod)method
                              URL:(NSString *)url
                           params:(NSDictionary *)dictParams
                 isShowNoNetAlert:(BOOL)isShowNoNetAlert
                         priority:(MPE_NetWorkPriority)priority
                          success:(MPHNetFinishedBlockStatic)success
                           failed:(MPHNetFinishedBlockStatic)failed
                           cached:(void (^)(NSDictionary *cachedObject))cached
{
    return [self netWorkWithMethod:method
                               URL:url
                            params:dictParams
                  isShowNoNetAlert:isShowNoNetAlert
      isShowToastView4NetWorkState:MPDefautShowToastState
                   timeoutInterval:MPDefaultRequestTimeoutInterval
                         isEncrypt:MPDefautIsEncrypt
                          priority:priority
                           success:success
                            failed:failed
                            cached:cached
                    sessionTimeOut:nil];
}

+ (instancetype)netWorkWithMethod:(MPE_NetMethod)method
                              URL:(NSString *)url
                           params:(NSDictionary *)dictParams
     isShowToastView4NetWorkState:(BOOL)isShowToastView4NetWorkState
                         priority:(MPE_NetWorkPriority)priority
                          success:(MPHNetFinishedBlockStatic)success
                           failed:(MPHNetFinishedBlockStatic)failed
                           cached:(void (^)(NSDictionary *cachedObject))cached
{
    return [self netWorkWithMethod:method
                               URL:url
                            params:dictParams
                  isShowNoNetAlert:MPDefautShowNoNetViewState
      isShowToastView4NetWorkState:isShowToastView4NetWorkState
                   timeoutInterval:MPDefaultRequestTimeoutInterval
                         isEncrypt:MPDefautIsEncrypt
                          priority:priority
                           success:success
                            failed:failed
                            cached:cached
                    sessionTimeOut:nil];
}

+ (instancetype)netWorkWithMethod:(MPE_NetMethod)method
                              URL:(NSString *)url
                           params:(NSDictionary *)dictParams
                 isShowNoNetAlert:(BOOL)isShowNoNetAlert
     isShowToastView4NetWorkState:(BOOL)isShowToastView4NetWorkState
                         priority:(MPE_NetWorkPriority)priority
                          success:(MPHNetFinishedBlockStatic)success
                           failed:(MPHNetFinishedBlockStatic)failed
{
    return [self netWorkWithMethod:method
                               URL:url
                            params:dictParams
                  isShowNoNetAlert:isShowNoNetAlert
      isShowToastView4NetWorkState:isShowToastView4NetWorkState
                   timeoutInterval:MPDefaultRequestTimeoutInterval
                         isEncrypt:MPDefautIsEncrypt
                          priority:priority
                           success:success
                            failed:failed
                            cached:nil
                    sessionTimeOut:nil];
}

+ (instancetype)netWorkWithMethod:(MPE_NetMethod)method
                              URL:(NSString *)url
                           params:(NSDictionary *)dictParams
                 isShowNoNetAlert:(BOOL)isShowNoNetAlert
     isShowToastView4NetWorkState:(BOOL)isShowToastView4NetWorkState
                         priority:(MPE_NetWorkPriority)priority
                          success:(MPHNetFinishedBlockStatic)success
                           failed:(MPHNetFinishedBlockStatic)failed
                           cached:(void (^)(NSDictionary *cachedObject))cached
{
    return [self netWorkWithMethod:method
                               URL:url
                            params:dictParams
                  isShowNoNetAlert:isShowNoNetAlert
      isShowToastView4NetWorkState:isShowToastView4NetWorkState
                   timeoutInterval:MPDefaultRequestTimeoutInterval
                         isEncrypt:MPDefautIsEncrypt
                          priority:priority
                           success:success
                            failed:failed
                            cached:cached
                    sessionTimeOut:nil];

}

+ (instancetype)netWorkWithMethod:(MPE_NetMethod)method
                              URL:(NSString *)url
                           params:(NSDictionary *)dictParams
                 isShowNoNetAlert:(BOOL)isShowNoNetAlert
     isShowToastView4NetWorkState:(BOOL)isShowToastView4NetWorkState
                  timeoutInterval:(CGFloat)timeoutInterval
                        isEncrypt:(BOOL)isEncrypt
                         priority:(MPE_NetWorkPriority)priority
                          success:(MPHNetFinishedBlockStatic)success
                           failed:(MPHNetFinishedBlockStatic)failed
                           cached:(void (^)(NSDictionary *cachedObject))cached
                   sessionTimeOut:(void (^)(MPE_NetWorkSessionState state))sessiontimeout
{
    MPBNetWorking *netWork = [[MPBNetWorking alloc] init];
    netWork.sessionTimeoutBlock = sessiontimeout;
    netWork.isShowToastView4NetWorkState = isShowToastView4NetWorkState;
    [netWork.netWorkBase setTimeoutInterval:timeoutInterval];
    
    switch (method)
    {
        case MPE_POST:
        {
            [self __post:netWork
        isShowNoNetAlert:isShowNoNetAlert
               isEncrypt:isEncrypt
                priority:priority
                     url:url
                  params:dictParams
                 success:success
                  failed:failed
                  cached:cached];
        }
            break;
        case MPE_GET:
        {
            
            [self __get:netWork
       isShowNoNetAlert:isShowNoNetAlert
              isEncrypt:isEncrypt
               priority:priority
                    url:url
                 params:dictParams
                success:success
                 failed:failed
                 cached:cached];
        }
            break;
    }
    
    return netWork;
}

+ (instancetype)netWorkWithMethod:(MPE_NetMethod)method
                              URL:(NSString *)url
                           params:(NSDictionary *)dictParams
                 isShowNoNetAlert:(BOOL)isShowNoNetAlert
     isShowToastView4NetWorkState:(BOOL)isShowToastView4NetWorkState
                  timeoutInterval:(CGFloat)timeoutInterval
                        isEncrypt:(BOOL)isEncrypt
                           oldApi:(BOOL)oldApi
                         priority:(MPE_NetWorkPriority)priority
                          success:(MPHNetFinishedBlockStatic)success
                           failed:(MPHNetFinishedBlockStatic)failed
                           cached:(void (^)(NSDictionary *cachedObject))cached
                   sessionTimeOut:(void (^)(MPE_NetWorkSessionState state))sessiontimeout
{
    MPBNetWorking *netWork;
    if (oldApi)
    {
        netWork = [[MPBNetWorking alloc] initWithApi:YES];
    }
    else
    {
        netWork = [[MPBNetWorking alloc] init];
    }
    netWork.sessionTimeoutBlock = sessiontimeout;
    netWork.isShowToastView4NetWorkState = isShowToastView4NetWorkState;
    [netWork.netWorkBase setTimeoutInterval:timeoutInterval];
    
    switch (method)
    {
        case MPE_POST:
        {
            [self __post:netWork
        isShowNoNetAlert:isShowNoNetAlert
               isEncrypt:isEncrypt
                priority:priority
                     url:url
                  params:dictParams
                 success:success
                  failed:failed
                  cached:cached];
        }
            break;
        case MPE_GET:
        {
            
            [self __get:netWork
       isShowNoNetAlert:isShowNoNetAlert
              isEncrypt:isEncrypt
               priority:priority
                    url:url
                 params:dictParams
                success:success
                 failed:failed
                 cached:cached];
        }
            break;
    }
    
    return netWork;
}

+ (void)__post:(MPBNetWorking *)netWork
isShowNoNetAlert:(BOOL)isShowNoNetAlert
     isEncrypt:(BOOL)isEncrypt
      priority:(MPE_NetWorkPriority)priority
           url:(NSString *)url params:(NSDictionary *)dictParams
       success:(MPHNetFinishedBlockStatic)success
        failed:(MPHNetFinishedBlockStatic)failed
        cached:(void (^)(NSDictionary *cachedObject))cached
{
    _mpqueue_block_async_on_networing_operate_queue(^
    {
        MPURLSessionTask *operation = [netWork.netWorkBase POST:url
                                                     parameters:dictParams
                                               isShowNoNetAlert:isShowNoNetAlert
                                                      isEncrypt:isEncrypt
                                                       priority:priority
                                                        success:^(MPURLSessionTask *operation, id responseObject)
                                       {
                                           switch ([[[[MPBNetWorkBase alloc] init] class] checkState4Result:responseObject
                                                                                                       task:operation
                                                                               isShowToastView4NetWorkState:[operation isShowToastView4NetWorkState]
                                                                                                     cached:cached
                                                                                        sessionTimeoutBlock:[operation sessionTimeoutBlock]])
                                           {
                                               case MPE_NetWorkState_Successs:
                                               {
                                                   MPHNetFinishedBlockStatic successTemp = success;
                                                   if (successTemp)
                                                   {
                                                       successTemp(operation, [[[[MPBNetWorkBase alloc] init] class] resetResponse:responseObject]);
                                                   }
                                                   break;
                                               }
                                               case MPE_NetWorkState_Fail:
                                               {
                                                   MPHNetFinishedBlockStatic failedTemp = failed;
                                                   if (failedTemp)
                                                   {
                                                       failedTemp(operation, responseObject);
                                                   }
                                                   break;
                                               }
                                               case MPE_NetWorkState_NoBack:
                                                   break;
                                           };
                                       }
                                                        failure:^(MPURLSessionTask *operation, NSError *error)
                                       {
                                           MPE_NetWorkState state = [[[[MPBNetWorkBase alloc] init] class] checkState4Result:nil
                                                                                                                        task:operation
                                                                                                isShowToastView4NetWorkState:[operation isShowToastView4NetWorkState]
                                                                                                                      cached:cached
                                                                                                         sessionTimeoutBlock:[operation sessionTimeoutBlock]];
                                           if (state != MPE_NetWorkState_NoBack)
                                           {
                                               MPHNetFinishedBlockStatic failedTemp = failed;
                                               if (failedTemp)
                                               {
                                                   failedTemp(operation, nil);
                                               }
                                           }
                                       }
                                                         cached:^(NSDictionary *cachedObject)
                                       {
                                           
                                           void (^cachedTemp)(NSDictionary *cachedObject) = cached;
                                           if (cachedTemp)
                                           {
                                               cachedTemp(cachedObject);
                                           }
                                       }];
        
        [netWork configOperaion:operation];
    
    });
}

+ (void)__get:(MPBNetWorking *)netWork
isShowNoNetAlert:(BOOL)isShowNoNetAlert
    isEncrypt:(BOOL)isEncrypt
     priority:(MPE_NetWorkPriority)priority
          url:(NSString *)url
       params:(NSDictionary *)dictParams
      success:(MPHNetFinishedBlockStatic)success
       failed:(MPHNetFinishedBlockStatic)failed
       cached:(void (^)(NSDictionary *cachedObject))cached
{
    
    _mpqueue_block_async_on_networing_operate_queue(^
    {
        MPURLSessionTask *operation = [netWork.netWorkBase GET:url
                                                    parameters:dictParams
                                              isShowNoNetAlert:isShowNoNetAlert
                                                     isEncrypt:isEncrypt
                                                      priority:priority
                                                       success:^(MPURLSessionTask *operation, id responseObject)
                                       {
                                           switch ([[[[MPBNetWorkBase alloc] init] class] checkState4Result:responseObject
                                                                                                       task:operation
                                                                               isShowToastView4NetWorkState:[operation isShowToastView4NetWorkState]
                                                                                                     cached:cached
                                                                                        sessionTimeoutBlock:[operation sessionTimeoutBlock]])
                                           {
                                               case MPE_NetWorkState_Successs:
                                               {
                                                   MPHNetFinishedBlockStatic successTemp = success;
                                                   if (successTemp)
                                                   {
                                                       successTemp(operation, [[[[MPBNetWorkBase alloc] init] class] resetResponse:responseObject]);
                                                   }
                                                   
                                                   break;
                                               }
                                                   
                                               case MPE_NetWorkState_Fail:
                                               {
                                                   MPHNetFinishedBlockStatic failedTemp = failed;
                                                   if (failedTemp)
                                                   {
                                                       failedTemp(operation, responseObject);
                                                   }
                                                   
                                                   break;
                                               }
                                                   
                                               case MPE_NetWorkState_NoBack:
                                                   
                                                   break;
                                           };
                                           
                                       }
                                                       failure:^(MPURLSessionTask *operation, NSError *error)
                                       {
                                           MPE_NetWorkState state = [[[[MPBNetWorkBase alloc] init] class] checkState4Result:nil
                                                                                                                        task:operation
                                                                                                isShowToastView4NetWorkState:[operation isShowToastView4NetWorkState]
                                                                                                                      cached:cached
                                                                                                         sessionTimeoutBlock:[operation sessionTimeoutBlock]];
                                           if (state != MPE_NetWorkState_NoBack)
                                           {
                                               MPHNetFinishedBlockStatic failedTemp = failed;
                                               if (failedTemp)
                                               {
                                                   failedTemp(operation, nil);
                                               }
                                           }
                                           
                                       }
                                                        cached:^(NSDictionary *cachedObject)
                                       {
                                           void (^cachedTemp)(NSDictionary *cachedObject) = cached;
                                           if (cachedTemp)
                                           {
                                               cachedTemp(cachedObject);
                                           }
                                       }];
        
        [netWork configOperaion:operation];
        
    });
}


#pragma mark - ******************************************* 类方法 - 同步网络请求方法 **********************

+ (id)netWorkSyncWithURL:(NSString *)url
                  params:(NSDictionary *)dictParams
                  cached:(void (^)(NSDictionary *cachedObject))cached
{
    return [self netWorkSyncWithURL:url
                             params:dictParams
                   isShowNoNetAlert:MPDefautShowNoNetViewState
       isShowToastView4NetWorkState:MPDefautShowToastState
                          isEncrypt:MPDefautIsEncrypt
                           priority:MPDefaultRequestPriority
                             cached:cached];
}

+ (id)netWorkSyncWithURL:(NSString *)url
                  params:(NSDictionary *)dictParams
        isShowNoNetAlert:(BOOL)isShowNoNetAlert
                  cached:(void (^)(NSDictionary *cachedObject))cached
{
     return [self netWorkSyncWithURL:url
                              params:dictParams
                    isShowNoNetAlert:isShowNoNetAlert
        isShowToastView4NetWorkState:MPDefautShowToastState
                           isEncrypt:MPDefautIsEncrypt
                            priority:MPDefaultRequestPriority
                              cached:cached];
}

+ (id)netWorkSyncWithURL:(NSString *)url
                  params:(NSDictionary *)dictParams
isShowToastView4NetWorkState:(BOOL)isShowToastView4NetWorkState
                  cached:(void (^)(NSDictionary *cachedObject))cached
{
    return [self netWorkSyncWithURL:url
                             params:dictParams
                   isShowNoNetAlert:MPDefautShowNoNetViewState
       isShowToastView4NetWorkState:isShowToastView4NetWorkState
                          isEncrypt:MPDefautIsEncrypt
                           priority:MPDefaultRequestPriority
                             cached:cached];
}

+ (id)netWorkSyncWithURL:(NSString *)url
                  params:(NSDictionary *)dictParams
        isShowNoNetAlert:(BOOL)isShowNoNetAlert
isShowToastView4NetWorkState:(BOOL)isShowToastView4NetWorkState
               isEncrypt:(BOOL)isEncrypt
                priority:(MPE_NetWorkPriority)priority
                  cached:(void (^)(NSDictionary *cachedObject))cached
{
    MPBNetWorking *netWork = [[MPBNetWorking alloc] init];
    netWork.isShowToastView4NetWorkState = isShowToastView4NetWorkState;
    
    return [netWork.netWorkBase netWorkSyncWithURL:url
                                            params:dictParams
                                  isShowNoNetAlert:isShowNoNetAlert
                                         isEncrypt:isEncrypt
                                          priority:priority
                                            cached:cached];
}


#pragma mark - ******************************************* 初始化方法 **********************

- (instancetype)init
{
    if (self = [super init]) 
    {
        _netWorkBase = [[MPBNetWorkBase alloc] init];
        _isShowToastView4NetWorkState = MPDefautShowToastState;
    }
    
    return self;
}

- (instancetype)initWithApi:(BOOL)isoldApi
{
    if (self = [super init])
    {
        _netWorkBase = [[MPBNetWorkBase alloc] initWithApi:isoldApi];
        _isShowToastView4NetWorkState = MPDefautShowToastState;
        
    }
    
    return self;
}

- (void)dealloc
{
    NSLog(@"======>>> %s", __func__);
}


#pragma mark - ******************************************* 私有方法 **********************

- (void)configeNetWorkFromOverload
{
    @autoreleasepool
    {
        if ([self respondsToSelector:@selector(netWorkParameters)])
        {
            [self.netWorkBase setOverloadParameters:[self netWorkParameters]];
        }
        
        if ([self respondsToSelector:@selector(netWorkHeaderCommentParameter)])
        {
            [self.netWorkBase setOverloadHeaderCommentParameters:[self netWorkHeaderCommentParameter]];
        }
        
        if ([self respondsToSelector:@selector(netWorkBodyCommentParameter)])
        {
            [self.netWorkBase setOverloadBodyCommentParameters:[self netWorkBodyCommentParameter]];
        }
        
        if ([self respondsToSelector:@selector(netWorkURL)])
        {
            [self.netWorkBase setRequestURL:[self netWorkURL]];
        }
        
        if ([self respondsToSelector:@selector(responseType)])
        {
            [self.netWorkBase setResponseType:[self responseType]];
        }
        
        if ([self respondsToSelector:@selector(setTimeoutInterval:)])
        {
            [self.netWorkBase setTimeoutInterval:self.timeoutInterval];
        }
        
    }
}

- (void)configOperaion:(MPURLSessionTask *)operation
{
    [operation setIsShowToastView4NetWorkState:_isShowToastView4NetWorkState];
    [operation setSessionTimeoutBlock:_sessionTimeoutBlock];
    [operation setReqUnique:_uniqueID];
}

- (void)netWorkSuccessPreHandleIfExist
{
    if ([self respondsToSelector:@selector(netWorkSuccessPreHandle)])
    {
        [self netWorkSuccessPreHandle];
    }
}

- (void)netWorkFailedPreHandleIfExist
{
    if ([self respondsToSelector:@selector(netWorkFailedPreHandle)])
    {
        [self netWorkFailedPreHandle];
    }
}

- (void)netWorkCachePreHandleIfExist
{
    if ([self respondsToSelector:@selector(netWorkCachePreHandle)])
    {
        [self netWorkCachePreHandle];
    }
}


@end
